package com.bydq.patrolsystem.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bydq.patrolsystem.annotation.MyLog;
import com.bydq.patrolsystem.entity.PsUser;
import com.bydq.patrolsystem.service.PsRoleService;
import com.bydq.patrolsystem.service.PsUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private PsUserService psUserService;

    @Autowired
    private PsRoleService psRoleService;

    @GetMapping("userUpdate")
    public String update(){
        return "userUpdate";
    }


    @MyLog("修改信息")
    @PostMapping("/userUpdate")
    @ResponseBody
    public String update(PsUser user, String password, String repassword, HttpSession session){
        //如果有错误则返回错误消息 成功就返回success  成功后更新当前session中的用户
        return  psUserService.ordinaryCheckAndUpdate(user,password,repassword,session);
    }


    @GetMapping("/userList")
    public String userList(){
        return "userList";
    }

    @MyLog("查看用户列表")
    @PostMapping("/userList")
    @ResponseBody
    public IPage<PsUser> userList(@RequestParam(value = "currentPage" , required = false , defaultValue = "1")
                                              Integer currentPage,
                                  @RequestParam(value = "pageSize" , required = false , defaultValue = "10")
                                              Integer pageSize,
                                  @RequestParam(value = "userName" ,required = false , defaultValue = "")
                                              String userName,
                                  @RequestParam(value = "userStatus" ,required = false , defaultValue = "")
                                              String userStatus){
        return psUserService.selectPageByEditions(currentPage,pageSize,userName,userStatus);
    }

    //添加
    @GetMapping("/operationUser")
    public String toUpdateOREdit(Model model){
        //获取当前启用的角色数组
        model.addAttribute("roles",psRoleService.listStartRole());
        return "addUser";
    }

    //编辑
    @GetMapping("/operationUser/{id}")
    public String toUpdateOREdit(@PathVariable Integer id,Model model){
        //获取当前启用的角色数组
        model.addAttribute("roles",psRoleService.listStartRole());
        model.addAttribute("updateUser",psUserService.getById(id));

        return "updateUser";
    }

    @MyLog("添加或删除角色")
    @PostMapping("/addOrUpdate")
    @ResponseBody
    public boolean addOrUpdate(PsUser user){
        return psUserService.saveOrUpdate(user);
    }

    @PostMapping("/userCodeExist/{userCode}")
    @ResponseBody
    public boolean userCodeExist(@PathVariable String userCode){
        return psUserService.userCodeExist(userCode);
    }

    @PostMapping("/setStart/{id}/{start}")
    @ResponseBody
    public boolean setStart(@PathVariable Integer id,@PathVariable Integer start){
        return psUserService.setUserStatus(id,start);
    }

    @MyLog("删除用户")
    @PostMapping("/del/{id}")
    @ResponseBody
    public boolean del(@PathVariable Integer id){
        return psUserService.removeById(id);
    }
}
